package cn.superchart.web.core.handler;

import cn.hutool.json.JSONUtil;
import cn.superchart.common.base.BaseResult;
import cn.superchart.common.enums.ExceptionEnum;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import java.io.IOException;

/**
 * 未认证用户 的异常（例如，用户未登录时尝试访问受保护的资源）。
 */
@Component
public class ScAuthenticationEntryPoint  implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        BaseResult<String> resResult = BaseResult.by(ExceptionEnum.TOKEN_FORBIDDEN,null);
        response.getWriter().write(JSONUtil.toJsonStr(resResult));
    }
}
